home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  4.3 KB  |  176 lines

  1. /*        Case-independent string comparison        HTString.c
  2. **
  3. **    Original version came with listserv implementation.
  4. **    Version TBL Oct 91 replaces one which modified the strings.
  5. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  6. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  7. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  8. */
  9. #include"capalloc.h"
  10. #include"capstdio.h"
  11. #include <ctype.h>
  12. #include "HTUtils.h"
  13. #include "tcp.h"
  14.  
  15. #ifndef RELEASE
  16. PUBLIC int WWW_TraceFlag = 0;    /* Global trace flag for ALL W3 code */
  17. #endif /* RELEASE */
  18.  
  19. #ifndef VC
  20. #define VC "unknown"
  21. #endif
  22.  
  23. PUBLIC CONST char * HTLibraryVersion = VC; /* String for help screen etc */
  24.  
  25. #ifndef VM        /* VM has these already it seems */
  26.     
  27. /*    Strings of any length
  28. **    ---------------------
  29. */
  30. PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
  31. {
  32.     CONST char *p =a;
  33.     CONST char *q =b;
  34.     for(p=a, q=b; *p && *q; p++, q++) {
  35.         int diff = TOLOWER(*p) - TOLOWER(*q);
  36.         if (diff) return diff;
  37.     }
  38.     if (*p) return 1;    /* p was longer than q */
  39.     if (*q) return -1;    /* p was shorter than q */
  40.     return 0;        /* Exact match */
  41. }
  42.  
  43.  
  44. /*    With count limit
  45. **    ----------------
  46. */
  47. PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
  48. {
  49.     CONST char *p =a;
  50.     CONST char *q =b;
  51.     
  52.     for(p=a, q=b;; p++, q++) {
  53.         int diff;
  54.         if (p == a+n) return 0;    /*   Match up to n characters */
  55.         if (!(*p && *q)) return *p - *q;
  56.         diff = TOLOWER(*p) - TOLOWER(*q);
  57.         if (diff) return diff;
  58.     }
  59.     /*NOTREACHED*/
  60. }
  61. #endif
  62.  
  63. extern char *HTSACopy(char **cpp_dest, const char *cp_src)    {
  64. /*
  65.  *    Purpose:    Allocate space for a string, then copy it.
  66.  *    Arguments:    cpp_dest    The destination string.
  67.  *                    If cpp_dest points to NULL,
  68.  *                    space will be allocated for it.
  69.  *                    If cpp_dest does not point to NULL,
  70.  *                    what it points to is freed, and
  71.  *                    then space is allocated for it.
  72.  *            cp_src        The source string to copy over.
  73.  *    Return Value:    char *    Should be the same as what cpp_dest points
  74.  *                to.
  75.  *    Remarks/Portability/Dependencies/Restrictions:
  76.  *        Space allocated here should be freed by the calling
  77.  *        function.
  78.  *    Revision History:
  79.  *        ??-??-??    created
  80.  *        03-28-94    modified for DosLynx
  81.  */
  82.  
  83.     /*
  84.      *    Does the destination point to any already allocated mem?
  85.      *    If so, free it.
  86.      */
  87.     if(*cpp_dest)    {
  88.         free(*cpp_dest);
  89.     }
  90.  
  91.     /*
  92.      *    If the source string is just null, so should the
  93.      *    destination.
  94.      */
  95.     if(cp_src == NULL)    {
  96.         *cpp_dest = NULL;
  97.     }
  98.     else    {
  99.         /*
  100.          *    The source string is not NULL.
  101.          *    Allocate space for the destination string and
  102.          *    copy it over.
  103.          */
  104.         *cpp_dest = (char *)malloc(strlen(cp_src) + 1);
  105.         if(*cpp_dest == NULL)    {
  106.             outofmem(__FILE__, "HTSACopy");
  107.         }
  108.         strcpy(*cpp_dest, cp_src);
  109.     }
  110.  
  111.     /*
  112.      *    Return the copy of the source string.
  113.      */
  114.     return(*cpp_dest);
  115. }
  116.  
  117. /*    String Allocate and Concatenate
  118. */
  119. PUBLIC char * HTSACat
  120.   ARGS2 (char **,dest, CONST char *,src)
  121. {
  122.   if (src && *src) {
  123.     if (*dest) {
  124.       int length = strlen (*dest);
  125.       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
  126.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  127.       strcpy (*dest + length, src);
  128.     } else {
  129.       *dest = (char *) malloc (strlen(src) + 1);
  130.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  131.       strcpy (*dest, src);
  132.     }
  133.   }
  134.   return *dest;
  135. }
  136.  
  137.  
  138. /*    Find next Field
  139. **    ---------------
  140. **
  141. ** On entry,
  142. **    *pstr    points to a string containig white space separated
  143. **        field, optionlly quoted.
  144. **
  145. ** On exit,
  146. **    *pstr    has been moved to the first delimiter past the
  147. **        field
  148. **        THE STRING HAS BEEN MUTILATED by a 0 terminator
  149. **
  150. **    returns    a pointer to the first field
  151. */
  152. PUBLIC char * HTNextField ARGS1(char **, pstr)
  153. {
  154.     char * p = *pstr;
  155.     char * start;            /* start of field */
  156.     
  157.     while(*p && WHITE(*p)) p++;        /* Strip white space */
  158.     if (!*p) {
  159.     *pstr = p;
  160.         return NULL;        /* No first field */
  161.     }
  162.     if (*p == '"') {            /* quoted field */
  163.         p++;
  164.     start = p;
  165.     for(;*p && *p!='"'; p++) {
  166.         if (*p == '\\' && p[1]) p++;    /* Skip escaped chars */
  167.     }
  168.     } else {
  169.     start = p;
  170.     while(*p && !WHITE(*p)) p++;    /* Skip first field */
  171.     }
  172.     if (*p) *p++ = 0;
  173.     *pstr = p;
  174.     return start;
  175. }
  176.